// pages/articlefavorite/articlefavorite.js
var util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    articleMain: [],
    currentPage: 1,
    offline: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得dialog组件
    this.loadmore = this.selectComponent("#loadmore");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    /****获取收藏列表****/
    this.getAtrticleMain(true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getAtrticleMain(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadmore.show();
    const pagenum = this.data.currentPage + 1
    const tabData = this.data.tabData;
    this.setData({
      currentPage: pagenum
    })
    this.getAtrticleMain(false);
  },
  getAtrticleMain: function (isRefresh) {//获取首页推荐列表
    const that = this;
    var pagenum = this.data.currentPage;
    var articleMain = this.data.articleMain;
    const url = "/apid/r/136/" + pagenum;
    const accountid = app.globalData.accountid;
    util.DoCommand(url, "accountid=" + accountid, null, function (data) {
      var pagenum = that.data.currentPage;
      if (data.length < app.globalData.pagesize && pagenum != 1) {
        pagenum = pagenum - 1;
      }
      if (isRefresh == true) {
        articleMain = data
      }
      else {
        //合并数据
        for (var i in data) {
          var _id = data[i].id;
          var hasCount = 0;//标识是否已存在
          for (var j in articleMain) {
            var _nid = articleMain[j].id;
            if (_nid == _id) {
              hasCount++;
            }
          }
          //如果不存在则追加数据
          if (hasCount < 1) {
            articleMain.push(data[i]);
          }
        }
      }
      that.setData({
        articleMain: articleMain,
        currentPage: pagenum
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.loadmore.hide();
    }, null, that)
  },
  tapArticle: function (e) {//点击文章
    //获取点击id
    var articleid = e.currentTarget.dataset.articleid;
    wx.navigateTo({
      url: '/pages/articledetail/articledetail?id=' + articleid
    })
  }
})